<template>
	<view class="ts-flex ts-column ts-flex-item">
		<ts-banner image="https://via.placeholder.com/750x500.jpg" height="250"></ts-banner>
		<ts-search-bar v-model="keywords" @search="doSearch"></ts-search-bar>
		<view class="ts-list">
			<view class="ts-list-cell" v-for="(item,index) in list" :key="index">
				<view class="ts-column ts-flex-item ts-padding">
					<view class="ts-h4 ts-text-bold">{{item.address}}</view>
					<view class="ts-padding-top">
						<view class="">{{item.time}}</view>
						<view class="ts-flex-item" style="justify-content: flex-end;">
							<!-- primary,warn,danger,success -->
							<view v-if="item.status.length>0" class="ts-padding-right">
								<ts-badge type="success" :text="item.status"></ts-badge>
							</view>

							<ts-badge :type="item.type==='丢失'?'warn':'primary'" :text="item.type"></ts-badge>
						</view>
					</view>
					<view class="ts-padding-bottom ts-padding-top">
						{{item.content}}
					</view>
					<view class="" style="flex-wrap: wrap;">
						<image v-for="(img,index1) in item.images" :key="index1" :src="img" mode="" style="width: 220upx; height: 220upx; padding-right: 10upx; padding-bottom: 10upx;"></image>
					</view>
				</view>
			</view>
		</view>

		<view class="ts-flex ts-column">
			<ts-load-more :loadingType="loadingType" @tap="getEventListMoreData"></ts-load-more>
		</view>

	</view>
</template>

<script>
	import tsBadge from 'components/teaset/components/ts-badge.vue';
	import tsBanner from 'components/teaset/components/ts-banner.vue';
	import tsSearchBar from 'components/teaset/components/ts-search-bar.vue';
	import tsLoadMore from 'components/teaset/components/ts-load-more.vue';

	var self;
	export default {
		components: {
			tsBadge,
			tsBanner,
			tsSearchBar,
			tsLoadMore
		},
		data: {
			loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
			keywords: '',
			page: 0,
			// api: 'https://www.easy-mock.com/mock/5bcd1a3d5f4b6e63fbb101cc/course/api/app/lost-found/getEventListByUser',
			api: '',
			list: []
		},
		methods: {
			init() {
				this.list = [];
				this.loadingType = 0;
				this.page = 0;
			},
			doSearch(keywords) {
				console.log(keywords);
				this.init();

				this.getEventListMoreData();
			},
			getEventListMoreData() {
				//上拉的状态：0-loading前；1-loading中；2-没有更多了
				if (this.loadingType !== 0) {
					return;
				}
				this.loadingType = 1;
				this.page = this.page + 1;

				uni.request({
					url: self.api,
					method: 'GET', //GET,POST,PUT,...
					data: {
						user_id: 1,
						page: self.page,
						keywords: self.keywords
					},
					dataType: 'json',
					success(response) {
						const res = response.data;

						console.log(JSON.stringify(res));
						//           {
						//             data:[],
						//             success:true,
						//             has_more:true,
						//             page:1,
						//             message:''
						//           }
						if (res.success) {
							const data = res.data;
							if (data) {
								self.list = self.list.concat(data);
							}

							if (res.has_more === false) {
								self.loadingType = 2; //2-没有更多了
							} else {
								self.loadingType = 0; //开启新一轮加载
							}
						} else {
							console.log(res.message);
						}
					}
				});
			}
		},
		onReachBottom() {
			console.log('onReachBottom');
			this.getEventListMoreData();
		},
		onLoad(e) {
			self = this;
			this.init();
			this.api = this.$API_BASE + '/app/lost-found/getEventListByUser';

			this.getEventListMoreData();
		}
	};
</script>

<style>
	view {
		display: flex;
		flex-direction: row;
	}
</style>
